<?php 
	
	//分析
	//1.  limit 3,5
	//2.  页码  上一页12345678下一页

	//已知条件
	// 1.每页显示几条数据   $num
	//	2. 总数据   $total

	/**
	*	定义分页类
	*/
	class Page
	{
		//成员属性
		public $num;
		public $total;
		public $p;

		//构造方法
		public function __construct($num,$total)
		{
			$this->num = $num;
			$this->total = $total;

			$this->p = isset($_GET['p']) ? $_GET['p']:1;

		}

		//成员方法
		/**
		*	获取limit  1 0,5  2 5,5  3 10,5 4 15,5
		*/
		public function getLimit()
		{
			//获取开始
			$start = ($this->p-1)*$this->num;
			//获取结束
			$end = $this->num;

			//拼接limit
			$limit = $start.','.$end;

			return $limit;
		}

		/**
		*	获取页码  上一页1234567下一页
		*/
		public function show()
		{
			//' => string '/lamp161/oop7/testPage.php' 
			$url = $_SERVER['SCRIPT_NAME'];

			//var_dump($url);

			//获取最大的页数
			$totalPage = ceil($this->total/$this->num);

			if($this->p <=1) {

				$prev = 1;

			} else {
				$prev = $this->p-1;
			}
			$str = '';
			//上一页
			$str .=  "<a href='$url?p=".$prev."'>上一页</a>";

			//页码 123456
			for ($i=1; $i <=$totalPage; $i++) { 

				if($this->p == $i) {

					$str .=  "<a href='$url?p=$i' style='color:orange'>第{$i}页</a>";
				} else {
					$str .= "<a href='$url?p=$i'>第{$i}页</a>";

				}
				
			}

			if($this->p >= $totalPage)
			{
				$next = $totalPage;
			} else {
				$next = $this->p+1;
			}
			//下一页

			$str .=  "<a href='$url?p=".$next."'>下一页</a>";

			return $str;
		}


	}

	//实例化对象
	//$page = new Page(5,38);

	// var_dump($page);
	//$page->getLimit();

	//echo $page->show();



